<?php

include_once("RSAEncrypt.class.php");

// $public_key = '-----BEGIN PUBLIC KEY-----
// MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPbpLAPP9Gnmt0sy/hqdif9gvX
// G5+A5wIcHt/Ga9zFWaH7RrkLm5bcs1tuzkVko6wnf45QGNlux702AMSsqVILp+QN
// OPHt0XDY4vosPhuK2Y3CcBbTB1owPHSF0G66GrxSOLOQAPYHCabP4k0ellrbEsEn
// hTfAk/mcRVOU12iXRwIDAQAB
// -----END PUBLIC KEY-----';
// $private_key = '-----BEGIN RSA PRIVATE KEY-----
// MIICXAIBAAKBgQDPbpLAPP9Gnmt0sy/hqdif9gvXG5+A5wIcHt/Ga9zFWaH7RrkL
// m5bcs1tuzkVko6wnf45QGNlux702AMSsqVILp+QNOPHt0XDY4vosPhuK2Y3CcBbT
// B1owPHSF0G66GrxSOLOQAPYHCabP4k0ellrbEsEnhTfAk/mcRVOU12iXRwIDAQAB
// AoGAa6lJU6uVFzMcNKrYHDkKbSPIS3JT3BSKPMwtew/6uxdlgkTXwFWV/KC+wQkg
// bTaiILIMzDfJirzy0LefXLpZETJwcGZJKwdgwrlMHdjRm3qqGj1VjpURxzqlpg2t
// tdGDb72kqiMA3J6vuETCZUi7E0hWQ9VebVV1vXNw0ipBmrECQQDoGm403IzIfNFZ
// G45hB6cstgY8BUHB5JlDzy5D+knvnPsHc5z65fMK7Ksy62u5y8edpZdujGfpGPuM
// p5hrgKQfAkEA5Mng1WzMvJwy/megyMD6zQE+BnJty8/50NVxiKX+ZRQ1JMxwxal8
// Oi5BlOhJlex/TaETDYXtylIm7N7pGAtn2QJAdOrjewNic5fUiROFfEEzqCUlLNTl
// E/vIbM0PnDSXS/mCxnUprYVqEaW4c5T9lgEd077XcluN+HCaPncqI3UQtwJBAKhq
// X2JcY94XkYYq3HXLcRPF7VWrmF1m7HupE5lGsBzSIkjDYeRvXe4qLMajs790R0Xq
// 426Z/zBnBxZpBOdZRgkCQHOw16BWBx/QKObXsn7PLq2LUmYfKQ92xlm3TyhQc7GN
// eLxgc72P7kPTCMwmcPJHe9zDsV6H/aF8AOerGNzzhPo=
// -----END RSA PRIVATE KEY-----';

$public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVSOFJ/KLje86sBqw+cjc++kJC
zjWldh5I9+kWQYT+dboFgf764TRGtz5lgRIgJjmtm5QdQI40b5QcZcWqMbJ/0s4G
W9JSQUm9RFOT8jrAQY72RhpCVjW79XFreWr9adL7IY9rkqa/Wyel9UATr5bt1H2M
XF86w5lAqg9TG9pXiQIDAQAB
-----END PUBLIC KEY-----';

$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDVSOFJ/KLje86sBqw+cjc++kJCzjWldh5I9+kWQYT+dboFgf76
4TRGtz5lgRIgJjmtm5QdQI40b5QcZcWqMbJ/0s4GW9JSQUm9RFOT8jrAQY72RhpC
VjW79XFreWr9adL7IY9rkqa/Wyel9UATr5bt1H2MXF86w5lAqg9TG9pXiQIDAQAB
AoGBAI5A+Lo/PWlah0LnhHR1+3EOu6kRHmjIenSSnS0R8CFD5hmMVcOA9zv/Nie/
SZaVyDyJmuVTzEUe8ntqScc1NGai2qxN6850O4QpH6S7XtohbifyDNaxVzk/6Tuw
dKOGSLJIK7Jgh3u0F8+7YzPEdUijdyZc3f7hGFPQzpPqqhwBAkEA9cc+YeRq54vI
H4979ADkhpHFEGb6fqziNBj3Jmm6X/Dt3zHX/buFS0nClqJdDWwyy9v8eqUpjyIg
KiLaxZQMgQJBAN4nrtGJDM24ry6kNgIhBHA3K70K+sdyZIuboeuKGMQJCsPcrh3r
HTnwGnZA/vKPk1+7gIyHLiXhjfQuh2GeZwkCQQCLSXhkGAQx5vjqUfGobd0VGoV4
7zNlGOs1JTTj0yVpe+wDAodq55v59/pMTgW4Nqpu9+QVSYbM9EUQPCKJuIaBAkBM
9vCQ9WszJoXCoe94I1o3SlXj45boXFBLMbsSMEDOQdex7ZWBFgCFHxL73x60iFtl
cj9h8D9jrlYQv8H1ZvyJAkEA9KlvAtTRwvmdyldBVMH4O6VjGIOHvyiW7G/ki0Yf
A4cRi+Yzdjympg0W8d3i0e5kCdogQ95d/KBbfCPoJIPAlg==
-----END RSA PRIVATE KEY-----';

$rsa = new \Org\RSAEncrypt();

$rsa->setPrivateKey($private_key);
$rsa->setPublicKey($public_key);


$txt = '你好';
$encrypted = $rsa->encrypt($txt);
$decrypted = $rsa->decrypt($encrypted);

// echo "encrypted: "; echo $encrypted; echo "\n";
// echo "decrypted: "; echo $decrypted; echo "\n";

// echo "h16: "; echo base64_decode($encrypted); echo "\n";
// $js_encrypted = 'N5xfYD24d147GzyYq7GgWhX7NO+pR+KQc+lvDCkbV289aUGAz2ZeAwt7ySYX4FPLBzKt14KBny4ub7vT9prItYhCSQ/eLOnFgS4ukCexZ30F5wu1dCXIsnAKk+unBtbKWJnjjPpN6SbB5LY7aKlpbsIJBWkFNruXPszOuutAvK8=';

// $js_decrypted = $rsa->decrypt($js_encrypted);
// echo "decrypted: "; echo $js_decrypted; echo "\n";

$js_encrypted = '49897975dc5746a1aebf0c935095fe4a1760b98681ac6ed81a01d3af434efb308ef7440859dd31bd361b51408706b9dd3a36f9292a0f36b5634df1920b88097c0adfe59e20e873c0cceb7adb266dffbd04bb36cf52b859ad03de37709f2afb1e4a4fd8a57e184e08d7bb3bfcf75f5b35e5ae6f27baca96a6510d8c5377befc18';
// echo base64_encode($js_encrypted); echo "\n";
$data = base64_encode($js_encrypted);
$data = 'NDk4OTc5NzVkYzU3NDZhMWFlYmYwYzkzNTA5NWZlNGExNzYwYjk4NjgxYWM2ZWQ4MWEwMWQzYWY0MzRlZmIzMDhlZjc0NDA4NTlkZDMxYmQzNjFiNTE0MDg3MDZiOWRkM2EzNmY5MjkyYTBmMzZiNTYzNGRmMTkyMGI4ODA5N2MwYWRmZTU5ZTIwZTg3M2MwY2NlYjdhZGIyNjZkZmZiZDA0YmIzNmNmNTJiODU5YWQwM2RlMzc3MDlmMmFmYjFlNGE0ZmQ4YTU3ZTE4NGUwOGQ3YmIzYmZjZjc1ZjViMzVlNWFlNmYyN2JhY2E5NmE2NTEwZDhjNTM3N2JlZmMxOA==';
$js_decrypted = $rsa->decrypt($data, true);
echo "base64 encode decrypted: "; echo $js_decrypted; echo "\n";
// echo "base642 encode decrypted: "; echo base64_decode(trim(utf8_strrev($js_decrypted))); echo "\n";
// var_dump('ZmFzZGZhc2RmYXNkZg=='==trim(utf8_strrev($js_decrypted)));
// $js_encrypted = unpack("H*", base64_decode($js_decrypted))

// echo "base64 cn: "; echo base64_encode('你好'); echo "\n";
// echo base64_decode('aGho6aG26aG26aG26aG2ZA=='); echo "\n";
function utf8_strrev($str){
    preg_match_all('/./us', $str, $ar);
    return join('',array_reverse($ar[0]));
}

// =Emndm+pkW+rYa+pkWOdyV2d
/**
 * 解析js加密的密码
 * @param type $encoded 加密后的密文
 * @param type $is_from_js 密码是否来自js
 * @return boolean
 */
function decodeRsa($encoded, $is_from_js = true) {
    if(defined('RSA_PEM')){
        $data = base64_encode(pack("H*", $encoded)); //转成base64格式
        $key_content = file_get_contents(RSA_PEM);
        $prikeyid    = openssl_get_privatekey($key_content);
        $data   = base64_decode($data);
        $padding = $is_from_js ? OPENSSL_NO_PADDING : OPENSSL_PKCS1_PADDING;
        if (openssl_private_decrypt($data, $sourcestr, $prikeyid, $padding)){
            return $is_from_js ? rtrim(strrev($sourcestr), "\0") : "".$sourcestr;
        }
        return false;
    }
    return false;
}

// define("RSA_PEM", "/Users/leeyi/workspace/encryption/test.pem"); //私钥文件
// //JS->PHP 测试
// $data = "2790fb541abd01c89bb299cd218cb98cfe8140522e3dd9cf580a74a251e482824757e188668f11f25ac5cf976ff418ddcebd098479b01784290a935d4c71719d86a6243e5509546cfdb97607615f567cd62fda523c9468a6041f73599ff0ddd4798fd0a49058d1dbcc00bbe8af174fe472e838ca6a493faa4b6851b8e62b3284";

// $txt_de = decodeRsa($data, TRUE);
// var_dump($txt_de);

